﻿/*********************************************************************************
 *Author:         OnClick
 *Version:        0.0.1
 *UnityVersion:   2017.2.3p3
 *Date:           2019-08-13
 *Description:    IFramework
 *History:        2018.11--
*********************************************************************************/
using UnityEngine;
using IFramework.AAEX;

namespace IFramework.Hotfix.Lua
{
    public class AddressableLoader : IXLuaLoader
    {
        public static string projectScriptsPath
        {
            get { return Application.dataPath.CombinePath("Project/Lua").ToRegularPath(); }
        }
        public byte[] load(ref string path)
        {
            path = path.Replace(".", "/");
            string filepath = path.Append(".lua");
            if (path.EndsWith(".lua")) filepath = path;
            var textAsset = Resources.Load<TextAsset>(filepath);
            if (textAsset != null)
                return textAsset.bytes;
            filepath = projectScriptsPath.CombinePath(filepath + ".txt").ToAssetsPath();
            var handle = Assets.LoadPreparedAsset<TextAsset>(filepath);
            textAsset = handle;
            if (textAsset == null) return null;
            var bytes = textAsset.bytes;
            Assets.Release(handle);
            return bytes;
        }
    }
}
